package com.lgc.webtest.controller;

import com.lgc.webtest.mq.producer.HelloSender;
import com.rabbitmq.client.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/testRabbitMq")
public class TestRabbitMqController {

    /*https://www.jianshu.com/p/da1befae642e
    RabbitMQ常用的Exchange Type有fanout、direct、topic、headers四种。*/
    @Autowired
    private HelloSender helloSender;

    @RequestMapping("/test")
    public String test() {
        helloSender.send("hello world");

        return "ok";
    }

    @RequestMapping("/sendMessage")
    public String sendMessage(String message) {

        for (int i=0;i<50;i++) {
            helloSender.send(message+" "+String.valueOf(i));
        }

        return "ok";
    }

    @RequestMapping("/sendMessage2")
    public String sendMessage2(String message) {

        helloSender.send(message);

        return "ok";
    }

    @RequestMapping("/send")
    public void send() throws Exception {
        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("115.159.201.228");
            factory.setUsername("lgc");
            factory.setPort(5672);//注意这里的端口与管理插件的端口不一样
            factory.setPassword("qazwsxedc");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            //声明一个direct模式的交换机
            channel.exchangeDeclare("test-exchange.direct", BuiltinExchangeType.DIRECT, true);
            //声明一个非持久化自动删除的队列
            channel.queueDeclare("hello", false, false, true, null);//如果该队列不在被使用就删除他 zhe
            //将绑定到改交换机
            channel.queueBind("hello", "test-exchange.direct", "test-routing-key");
            //声明一个消息头部
            Map<String, Object> header = new HashMap<>();
            AMQP.BasicProperties.Builder b = new AMQP.BasicProperties.Builder();
            header.put("charset", "utf-8");
            b.headers(header);
            AMQP.BasicProperties bp = b.build();
            //将消息发出去
            channel.basicPublish("test-exchange.direct", "test-routing-key", false, bp, "test3".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/consume")
    public void consume() throws Exception {
        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("115.159.201.228");
            factory.setUsername("lgc");
            factory.setPort(5672);//注意这里的端口与管理插件的端口不一样
            factory.setPassword("qazwsxedc");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            //声明一个dirent模式的交换机
            channel.exchangeDeclare("test-exchange.direct",BuiltinExchangeType.DIRECT,true);
            //声明一个非持久化自动删除的队列
            channel.queueDeclare("hello",false,false,true,null);//如果该队列不在被使用就删除他 zhe
            //将绑定到改交换机
            channel.queueBind("hello","test-exchange.direct","test-routing-key");
            Consumer consumer = new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                                           byte[] body) throws IOException {
                    String message = new String(body, "UTF-8");
                    System.out.println(" [x] Received '" + message + "'");
                }
            };
            channel.basicConsume("hello", true, consumer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
